package com.bjsxt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**双向通信  单独创建两个线程，一个读，一个写，而主线程用来启动
 * @服务端
 * @线程*/
/*发送消息线程*/
class Send extends Thread{
    private Socket socket;
    public Send(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
        this.sendMsg();
    }
    //送消息
    private void sendMsg(){
        //发送消息需要键盘输入
        Scanner scanner = null;
        PrintWriter pw = null;//写出流
        try{
            //创建输入对象
            scanner = new Scanner(System.in);//输入要发送的信息
            //创建向对方输出消息的流对象  确定输出的方式
            pw = new PrintWriter(this.socket.getOutputStream());
            //不停发送消息
            while (true) {
                String keyInput = scanner.nextLine();
                pw.println(keyInput);//写给对方
                pw.flush();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(scanner != null){
                scanner.close();
            }
            if(this.socket != null){
                try {
                    this.socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(pw != null){
                pw.close();
            }
        }
    }
}
/*接收消息线程*/
class Receive extends Thread{
    //把Socket传进来
    private Socket socket;
    public Receive(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
        this.receiveMsg();
    }
    //接收消息
    private void receiveMsg(){
        BufferedReader br = null;
        try{
            //创建对象
            br = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            //一直接收消息
            while (true){
                String msg = br.readLine();
                System.out.println("他说："+msg);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(this.socket != null){
                try {
                    this.socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
public class ChatSocketServer {
    public static void main(String[] args) {
        //创建ServerSocket对象，ServerSocket在客户端与服务端连接后就关闭，
        //不会断开连接，因为ServerSocket只是用来创建连接，连接后由Socket保持连接
        //所以这里不可以关闭Socket
        ServerSocket serverSocket = null;
        try{
            serverSocket = new ServerSocket(6666);
            System.out.println("服务端启动，等待连接。。。。。");
            Socket socket = serverSocket.accept();
            System.out.println("连接成功");
            //启动线程
            new Send(socket).start();
            new Receive(socket).start();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(serverSocket != null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
